package Foundation;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import DomainPackage.SatisElemani;
import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;
import DomainPackage.*;

public class PilotMapper implements IMapper {

	@Override
	public Object get(int ID) {
		Pilot pilot = new Pilot(-1);
		ResultSet rs = DataConnector.getInstance().query("select * from pilot where pilotid = " + ID + "");
		
		if (rs != null){
            
            try {
                    
                    
            		rs.next();
                    pilot.setPilotID(Integer.parseInt(rs.getString(1)));
                    pilot.setAdi(rs.getString(2));
                    pilot.setSoyadi(rs.getString(3));
                    pilot.setDogumTarihi(new GregorianCalendar(Integer.parseInt(rs.getString(4).split("-")[0]), 
                    							Integer.parseInt(rs.getString(4).split("-")[1]), 
                    							Integer.parseInt(rs.getString(4).split("-")[2])));
            		
                    
                             return pilot;
                   
            } catch (NumberFormatException e) {
                    System.out.println("PilotMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("PilotMapper: sql problem");
                    e.printStackTrace();
            }
    }
    else
            System.out.println("no pilot with given ID");
		return null;
	}

	@Override
	public int put(Object object) {
		Pilot pilot=(Pilot) object;
			String dt = "";
			GregorianCalendar dta = pilot.getDogumTarihi();
			dt += String.valueOf(dta.get(GregorianCalendar.YEAR)) + "-";
			dt += String.valueOf(dta.get(GregorianCalendar.MONTH)) + '-';
			dt += String.valueOf(dta.get(GregorianCalendar.DAY_OF_MONTH));
            if(!DataConnector.getInstance().update("INSERT INTO pilot "+
                    "(adi, soyadi, pilotid, dogumtarihi) " +
                    "values ('"+pilot.getAdi()+"', '"+
                    pilot.getSoyadi()+"', '"+
                    pilot.getPilotID()+"', '"+
                    dt+"')")){
            	System.out.println("error");
            }
			
         return 0;

	}

	@Override
	public List<Object> getAll() {
        	List<Object> allPilotList = new ArrayList<Object>();
        	ResultSet rs = DataConnector.getInstance().query("select * from pilot;");
        	int numberOfRows=0;
    		try {
    			ResultSetMetaData rsMetaData = rs.getMetaData();
                numberOfRows = rsMetaData.getColumnCount()+1;
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		}
        try {
        	if (rs != null){
                while(rs.next()){
                        Pilot pilot= new Pilot(-1);       
                        		pilot.setPilotID(Integer.parseInt(rs.getString(1)));
                        		pilot.setAdi(rs.getString(2));
                        		pilot.setSoyadi(rs.getString(3));
                        		pilot.setDogumTarihi(new GregorianCalendar(Integer.parseInt(rs.getString(4).split("-")[0]), 
            										Integer.parseInt(rs.getString(4).split("-")[1]), 
            										Integer.parseInt(rs.getString(4).split("-")[2])));
                        		
                                allPilotList.add(pilot);             
                }
        	}
        }
        catch(Exception e){
        	e.printStackTrace();
        }
        	return allPilotList;
		
	}

}
